package com.wb.oldwomen.module;

import android.util.Log;
import android.view.View;

import com.wb.fw.Global;
import com.wb.fw.WorldCamera;
import com.wb.fw.WorldObject;
import com.wb.oldwomen.GamePlayLayer;
import com.wb.oldwomen.Ioldwomam;
import com.wb.oldwomen.R;
import com.wb.oldwomen.R.raw;
import com.wiyun.engine.afcanim.MWSprite;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.types.WYPoint;
import com.wiyun.engine.types.WYRect;

/**
 * 所有spirte都有一个实体，在 需要的时候，摆放位置。
 * 
 * @author Administrator
 * 
 */
public class Obj extends WorldObject implements Ioldwomam {
	int mtype;
	MWSprite mmwsprite;
	Sprite msprite, msprites[];
	GamePlayLayer mparent;
	boolean misVisible;

	public Obj(GamePlayLayer parent, int type) {
		mparent = parent;
		mtype = type;
		switch (type) {
		case OBJ_TYPE_APPLE:
			msprites = new Sprite[APPLE_LEN];
			for (int i = 0; i < APPLE_LEN; i++) {
				if (i == APPLE_POS) {
					msprites[i] = Sprite.make(R.drawable.obj_apple);
				} else {
					msprites[i] = Sprite.make(R.drawable.obj_gold);
				}
				msprites[i].autoRelease();
				msprites[i].setScale(Global.sScaleRate);
				mparent.addChild(msprites[i], Z_ROAD);
			}
			break;
		case OBJ_TYPE_HOUSE1:
			msprite = Sprite.make(R.drawable.obj_house1);
			msprite.autoRelease();
			msprite.setScale(Global.sScaleRate);
			msprite.setAnchor(0.5f, 0f);
			mparent.addChild(msprite, Z_OBJ_BACK);
			break;
		case OBJ_TYPE_GOLD:
			msprites = new Sprite[GOLD_LEN];
			for (int i = 0; i < GOLD_LEN; i++) {
				msprites[i] = Sprite.make(R.drawable.obj_gold);
				msprites[i].autoRelease();
				msprites[i].setScale(Global.sScaleRate);
				mparent.addChild(msprites[i], Z_ROAD);
			}
			break;
		case OBJ_TYPE_ONE_BOX:
			msprite = Sprite.make(R.drawable.obj_box);
			msprite.autoRelease();
			msprite.setScale(Global.sScaleRate);
			msprite.setAnchor(0.5f, 0f);
			mparent.addChild(msprite, Z_ROAD);
			break;
		case OBJ_TYPE_TARGET:
			msprites = new Sprite[2];
			msprites[0] = Sprite.make(R.drawable.obj_targetb);
			msprites[0].autoRelease();
			msprites[0].setScale(Global.sScaleRate);
			msprites[0].setAnchor(1f, 0f);
			mparent.addChild(msprites[0], Z_OBJ_BACK);
			
			msprites[1] = Sprite.make(R.drawable.obj_targetf);
			msprites[1].autoRelease();
			msprites[1].setScale(Global.sScaleRate);
			msprites[1].setAnchor(0f, 0f);
			mparent.addChild(msprites[1], Z_IGM_BTN);
			break;
		case OBJ_TYPE_TREE:
			msprite = Sprite.make(R.drawable.obj_tree);
			msprite.autoRelease();
			msprite.setScale(Global.sScaleRate);
			msprite.setAnchor(0.5f, 0f);
			mparent.addChild(msprite, Z_OBJ_BACK);
			break;
		case OBJ_TYPE_TWO_BOX:
			msprite = Sprite.make(R.drawable.obj_boxes);
			msprite.autoRelease();
			msprite.setAnchor(0.5f, 0f);
			msprite.setScale(Global.sScaleRate);
			mparent.addChild(msprite, Z_ROAD);
			break;
		case OBJ_TYPE_XIEPO:
			Texture2D tex = Texture2D.makePNG(R.drawable.obj_xiepo);
			tex.autoRelease();
			mmwsprite = MWSprite.make(R.raw.xiepo, 0, tex);
			mmwsprite.autoRelease();
			mmwsprite.setScale(Global.sScaleRate);
			mparent.addChild(mmwsprite, Z_ROAD);
			break;
		}
		setVisible(false, null);
	}

	@Override
	public void update(float delta) {
		if (!misVisible)
			return;
		float offset = -WorldCamera.getInstance().getFrameOffsetX();

		switch (mtype) {
		case OBJ_TYPE_APPLE:
			for (int i = 0; i < APPLE_LEN; i++) {
				if (msprites[i].isVisible()) {
					msprites[i].setPosition(
							msprites[i].getPositionX() + offset,
							msprites[i].getPositionY());
					if (i == APPLE_POS) {
						// Log.d("xltest", "it's apple");
					}
					if (checkCollision(msprites[i],
							mparent.mwoman.getScreenCollisionRect())) {
						msprites[i].setVisible(false);
						mparent.getBonus(i == APPLE_POS ? BONUS_APPLE
								: BONUS_GOLD);
					} else {
						if (i == APPLE_POS
								&& checkCollision(msprites[i],
										mparent.menemy.getWorldCollisionRect())) {
							msprites[i].setVisible(false);
						}
					}
				}
			}
			break;
		case OBJ_TYPE_GOLD:
			for (int i = 0; i < GOLD_LEN; i++) {
				if (msprites[i].isVisible()) {
					msprites[i].setPosition(
							msprites[i].getPositionX() + offset,
							msprites[i].getPositionY());
					if (checkCollision(msprites[i],
							mparent.mwoman.getScreenCollisionRect())) {
						msprites[i].setVisible(false);
						mparent.getBonus(BONUS_GOLD);
					}
				}
			}
			break;
		case OBJ_TYPE_ONE_BOX:
			if (msprite.isVisible()) {
				msprite.setPosition(msprite.getPositionX() + offset,
						msprite.getPositionY());
				if (checkLeftCollision(mparent.mwoman.getScreenCollisionRect())) {
//					mparent.mwoman.enableMove(this, false);
//					mparent.mwoman.switchState(STATE_COLLISION);
					mparent.mwoman.setLeftCollisionObj(this);
				} else {
					if (checkCollision(msprite,
							mparent.mwoman.getScreenCollisionRect())) {
						mparent.mwoman.setCollisionObj(this);
					} else {
//						mparent.mwoman.setCollisionObj(null);
					}

				}
//				else {
//					mparent.mwoman.enableMove(this, true);
//				}
			}
			break;
		case OBJ_TYPE_HOUSE1:
		case OBJ_TYPE_TREE:
			if (msprite != null && msprite.isVisible()) {
				// /20f
				msprite.setPosition(msprite.getPositionX() + offset / 2f,
						msprite.getPositionY());
				if (msprite.getPositionX() < -BASE_VERSION_SCREEN_WIDTH / 2) {
					msprite.setVisible(false);
				}
			}
			break;
		case OBJ_TYPE_TARGET:
			if (msprites[0] != null && msprites[0].isVisible()) {
				msprites[0].setPosition(msprites[0].getPositionX() + offset,
						msprites[0].getPositionY());
				msprites[1].setPosition(msprites[1].getPositionX() + offset,
						msprites[1].getPositionY());
				if (mparent.mwoman.mposx > mposx + BASE_VERSION_SCREEN_WIDTH/3*Global.sScaleRate) {
					mparent.mwoman.switchState(STATE_STOP);
				}
				if (mparent.menemy.mposx > mposx + BASE_VERSION_SCREEN_WIDTH/3*Global.sScaleRate) {
					Log.d("xltest", "checkWorldCollision, target....");
					mparent.menemy.stop();
				}
			}
			break;
		case OBJ_TYPE_XIEPO:
			if (mmwsprite != null && mmwsprite.isVisible()) {
				mmwsprite.setPosition(mmwsprite.getPositionX() + offset,
						mmwsprite.getPositionY());
				if (mposx < WorldCamera.getInstance().getCameraPosx()
						- BASE_VERSION_SCREEN_WIDTH) {
					mmwsprite.setVisible(false);
				}
				if (checkLeftWMCollision(mparent.mwoman.getScreenCollisionRect())) {
//					mparent.mwoman.enableMove(this, false);
//					mparent.mwoman.switchState(STATE_COLLISION);
					mparent.mwoman.setLeftCollisionObj(this);
				} else {
					if (checkMWCollision(mparent.mwoman
							.getScreenCollisionRect())) {
						mparent.mwoman.setCollisionObj(this);
					} else {
//						mparent.mwoman.setCollisionObj(null);
					}
				}
//				else {
//					mparent.mwoman.enableMove(this, true);
//				}
			}
			break;
		case OBJ_TYPE_TWO_BOX:
			if (msprite.isVisible()) {
				msprite.setPosition(msprite.getPositionX() + offset,
						msprite.getPositionY());
				if (checkLeftCollision(mparent.mwoman.getScreenCollisionRect())) {
//					mparent.mwoman.enableMove(this, false);
//					mparent.mwoman.switchState(STATE_COLLISION);
					mparent.mwoman.setLeftCollisionObj(this);
				} else {
					if (checkCollision(msprite,
							mparent.mwoman.getScreenCollisionRect())) {
						mparent.mwoman.setCollisionObj(this);
					} else {
//						mparent.mwoman.setCollisionObj(null);
					}
				}
//				else {
//					mparent.mwoman.enableMove(this, true);
//				}
			}
			break;
		}
	}

	boolean checkCollision(Sprite sprite, WYRect rect1) {
		WYRect rect = sprite.getTextureRect();
		rect = WYRect.make(sprite.getPositionX() + rect.minX(),
				sprite.getPositionY() + rect.minY(), rect.maxX() - rect.minX(),
				rect.maxY() - rect.minY());
		return rect1.isIntersect(rect);
	}
	
//	boolean checkWorldCollision(Sprite sprite, WYRect rect1) {
//		WYRect rect = sprite.getTextureRect();
//		Log.d("xltest", "x=" + (mposx + rect.minX()) + ",w="
//				+ (rect.maxX() - rect.minX()) + ",x1=" + rect1.minX() + ",w1="
//				+ (rect1.maxX() - rect1.minX()));
//		rect = WYRect.make(mposx + rect.minX(),
//				sprite.getPositionY() + rect.minY(), rect.maxX() - rect.minX(),
//				rect.maxY() - rect.minY());
//		
//		return rect1.isIntersect(rect);
//	}
	
	boolean checkCollision(WYRect rect1) {
		Sprite sprite = msprite;
		WYRect rect = sprite.getTextureRect();
		rect = WYRect.make(sprite.getPositionX() + rect.minX(),
				sprite.getPositionY() + rect.minY(), rect.maxX() - rect.minX(),
				rect.maxY() - rect.minY());
		return rect1.isIntersect(rect);
	}
	
	boolean checkMWCollision(WYRect rect1) {
		WYRect rect = mmwsprite.getCollisionRect(0);
		rect = WYRect.make(mmwsprite.getPositionX() + rect.minX(),
				mmwsprite.getPositionY() + rect.minY(), rect.maxX() - rect.minX(),
				rect.maxY() - rect.minY());
		return rect1.isIntersect(rect);
	}

	boolean checkLeftCollision(WYRect rect1) {
		Sprite sprite = msprite;
		WYRect rect = sprite.getTextureRect();
		// sprite.getAnchorX();
		// sprite.getAnchorY();
		// Log.d("xltest",
		// "rect="+rect.minX()+","+rect.minY()+","+rect.maxX()+","+rect.maxY());
		// 相交了 + woman最大的x>=最小的x
		float woman_miny = rect1.minY();
		float woman_maxx = rect1.maxX();
		float sprite_maxy = sprite.getPositionY() + rect.maxY()
				* (1 - sprite.getAnchorY());
		float sprite_minx = sprite.getPositionX() + rect.minX()
				- sprite.getAnchorX() * rect.maxX();
		// rect = WYRect.make(sprite.getPositionX() + rect.minX(),
		// sprite.getPositionY() + rect.minY(), rect.maxX() - rect.minX(),
		// rect.maxY() - rect.minY());
		rect = WYRect.make(
				sprite.getPositionX() + rect.minX() - sprite.getAnchorX()
						* rect.maxX(), sprite.getPositionY() + rect.minY(),
				rect.maxX() - rect.minX(), rect.maxY() - rect.minY());
//		if (mtype == OBJ_TYPE_ONE_BOX) {
//			Log.d("xltest", "woman_maxx=" + woman_maxx + ",sprite_minx="
//					+ sprite_minx + "," + (woman_maxx - sprite_minx)
//					+ ",rect1.isIntersect(rect)=" + rect1.isIntersect(rect));
//		}
		return rect1.isIntersect(rect) && woman_maxx - sprite_minx <= (JUMP_X_SPEED*1.2)*Global.sScaleRate;
	}
	
	boolean checkLeftWMCollision(WYRect rect1) {
		MWSprite sprite = mmwsprite;
		WYRect rect = sprite.getCollisionRect(0);
		// sprite.getAnchorX();
		// sprite.getAnchorY();
		// Log.d("xltest",
		// "rect="+rect.minX()+","+rect.minY()+","+rect.maxX()+","+rect.maxY());
		// 相交了 + woman最大的x>=最小的x
		float woman_miny = rect1.minY();
		float woman_maxx = rect1.maxX();
		float sprite_maxy = sprite.getPositionY() + rect.maxY()
				* (1 - sprite.getAnchorY());
		float sprite_minx = sprite.getPositionX() + rect.minX()
				- sprite.getAnchorX() * rect.maxX();
		// rect = WYRect.make(sprite.getPositionX() + rect.minX(),
		// sprite.getPositionY() + rect.minY(), rect.maxX() - rect.minX(),
		// rect.maxY() - rect.minY());
		rect = WYRect.make(
				sprite.getPositionX() + rect.minX() - sprite.getAnchorX()
						* rect.maxX(), sprite.getPositionY() + rect.minY(),
				rect.maxX() - rect.minX(), rect.maxY() - rect.minY());
//		if (mtype == OBJ_TYPE_ONE_BOX) {
//			Log.d("xltest", "woman_maxx=" + woman_maxx + ",sprite_minx="
//					+ sprite_minx + "," + (woman_maxx - sprite_minx)
//					+ ",rect1.isIntersect(rect)=" + rect1.isIntersect(rect));
//		}
		return rect1.isIntersect(rect) && woman_maxx - sprite_minx <= (JUMP_X_SPEED*1.2)*Global.sScaleRate;
	}

	// boolean checkCollision(MWSprite sprite) {
	// return true;
	// }

	public boolean isVisible() {
		return misVisible;
	}

	/**
	 * @param visible
	 * @param p
	 *            ,非缩放后坐标。
	 */
	public void setVisible(boolean visible, WYPoint p) {
		misVisible = true;
		float posy = 0, posx = 0;
		if (p != null) {
			posx = p.x;
			posy = p.y;
		}
		switch (mtype) {
		case OBJ_TYPE_APPLE:
			// int midheight=GLOD_MIN_Y+APPLE_POS*GLOD_SPACE_Y;
			for (int i = 0; i < APPLE_LEN; i++) {
				msprites[i].setVisible(visible);
				if (visible) {
					posy = p.y - Math.abs(APPLE_POS - i) * GLOD_SPACE_Y/3;
					if (posy < GLOD_MIN_Y) {
						posy = GLOD_MIN_Y;
					}
					posx = p.x - (APPLE_POS - i) * GLOD_SPACE_X;
					msprites[i].setPosition(posx * Global.sScaleRate, posy
							* Global.sScaleRate);
					Log.d("xltest", "set=" + posx * Global.sScaleRate + ",get="
							+ msprites[i].getPositionX() + ",scale="
							+ msprites[i].getScaleX());
				}
			}
			break;
		case OBJ_TYPE_GOLD:
			for (int i = 0; i < GOLD_LEN; i++) {
				msprites[i].setVisible(visible);
				if (visible) {
					int abs = Math.abs(APPLE_POS - i);
					posy = p.y - abs * abs * GLOD_SPACE_Y;
					if (posy < GLOD_MIN_Y) {
						posy = GLOD_MIN_Y;
					}
					posx = p.x - (APPLE_POS - i) * GLOD_SPACE_X;
					msprites[i].setPosition(posx * Global.sScaleRate, posy
							* Global.sScaleRate);
				}
			}
			break;
		case OBJ_TYPE_HOUSE1:
		case OBJ_TYPE_ONE_BOX:
		case OBJ_TYPE_TREE:
		case OBJ_TYPE_TWO_BOX:
			msprite.setVisible(visible);
			if (visible) {
				msprite.setPosition(posx * Global.sScaleRate, posy
						* Global.sScaleRate);
			}
			break;
		case OBJ_TYPE_XIEPO:
			mmwsprite.setVisible(visible);
			if (visible) {
				mmwsprite.setPosition(posx * Global.sScaleRate, posy
						* Global.sScaleRate);
			}
			break;
		case OBJ_TYPE_TARGET:
			msprites[0].setVisible(visible);
			msprites[1].setVisible(visible);
			if (visible) {
				msprites[0].setPosition(posx * Global.sScaleRate, posy
						* Global.sScaleRate);
				msprites[1].setPosition(posx * Global.sScaleRate, posy
						* Global.sScaleRate);
			}
			break;
		}
	}
	
	public int getType() {
		return mtype;
	}
	/////////
}
